
window.requestAnimFrame = (function() {
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
            return window.setTimeout(callback, 1000 / 60);
        };
})();
var Niubi = function(width, height, element, object){
    this.sprite = [];
    this.buliding = [];
    this.width = element.width = width;
    this.height = element.height = height;
    this.ctx = element.getContext("2d");
    this.preload = object.preload;
    this.create = object.create;
    this.update = object.update;
    this.render = object.render;
    this.score = 0;
    this.gamePlay = false;
    this.gameOver = false;
    this.success = false;
    this.audio = false;
};
Niubi.prototype.loadImage = function(name, address){
    var loadComplete = false;
    var newImg = new Image;
    newImg.onload = function(){
        loadComplete = true;
    };
    newImg.src = address;
    //while(!loadComplete) {}
    console.log("加载完成：" + name);

    if(typeof this[name] == "undefined"){
        this[name] = [];
    }
    this[name].push(newImg);

    //var T = setInterval(function(){
    //    if(!loadComplete){
    //        console.log("正在加载：" + name);
    //    }else{
    //        console.log("加载完成：" + name);
    //        clearInterval(T);
    //    }
    //}, 1);

};
Niubi.prototype.addSprite = function(x, y, obj, w, h){
    var spriteObj = function(x, y, obj){
        this.image = obj;
        this.currentImage = 0;
        this.movement = false;
        this.color = "black";
        this.x = x;
        this.y = y;
        this.w = w ||obj.width;
        this.h = h || obj.height;
        this.angle = 0;
        this.rotation = 0;
        this.alive = true;
        this.inWorld = true;
        this.a = 0;
        this.gravity = 0;
        this.speedX = 0;
        this.speedY = 0;
    };
    var newSprite = new spriteObj(x, y, this[obj]);
    this.sprite.push(newSprite);
    return newSprite;
};
Niubi.prototype.addBuilding = function(x, y, obj, method){
    var buildingObj = function(x, y, obj, method){
        this.image = obj;
        this.draw = method;
        this.color = obj.color;
        this.currentImage = 0;
        this.movement = false;
        this.x = x;
        this.y = y;
        this.w = obj.width;
        this.h = obj.height;
        this.marginRight = obj.marginRight;
        this.angle = 0;
        this.rotation = 0;
        this.alive = true;
        this.inWorld = true;
        this.gravity = 0;
        this.speedX = 0;
        this.speedY = 0;
    };
    var newBuild = new buildingObj(x, y, obj, method);
    this.buliding.push(newBuild);
    return newBuild;
};
var count = 0;
Niubi.prototype.gameLoop = function(){
    var This = this;
    var gameLoop = function(){
        count++;
        This.T = requestAnimFrame(gameLoop);
        if(!This.gameOver){
            if(This.success) return This.drawText();
            This.update();
            This.render();
            This.ctx.clearRect(0, 0, This.width, This.height);
            This.drawText();
            var i = 0;
            for(; i < This.sprite.length; i++){
                var currentSprite = This.sprite[i];
                currentSprite.speedX += currentSprite.a;
                currentSprite.speedY += currentSprite.gravity;
                currentSprite.x += currentSprite.speedX;
                currentSprite.y += currentSprite.speedY;
                This.ctx.drawImage(currentSprite.image[currentSprite.currentImage], currentSprite.x, currentSprite.y, currentSprite.w, currentSprite.h);
                if(currentSprite.movement){
                    if(count%5 == 0){
                        currentSprite.currentImage++;
                        if(currentSprite.currentImage >= currentSprite.image.length){
                            currentSprite.currentImage = 1;
                        }
                    }
                }else{
                    currentSprite.currentImage = 0;
                }
            }
            for(i = 0; i < This.buliding.length; i++){
                var currentBuild = This.buliding[i];
                currentSprite.speedX += currentSprite.a;
                currentSprite.speedY += currentSprite.gravity;
                currentBuild.x += currentBuild.speedX;
                currentBuild.y += currentBuild.speedY;
                currentBuild.draw(This.ctx);
            }
        }else{
            if(count%100 == 0) hint.style.display = "block";
        }
    };
    gameLoop();
};
Niubi.prototype.drawText = function(){
    this.ctx.save();
    //分数显示
    this.ctx.fillStyle = "#000000";
    this.ctx.font = "22px YouYuan";
    this.ctx.fillText("玩法介绍:", 600, 50);
    //玩法介绍
    this.ctx.fillText("请对着麦克风狂吼！", 600, 80);
    this.ctx.fillText("没有麦克？", 600, 110);
    this.ctx.fillText("请使用Enter键", 600, 140);
    //提示游戏结束
    if(this.success) {
        if(!this.alpha) this.alpha = 0.01;
        this.alpha += 0.005;
        if(this.alpha > 1) this.alpha = 1;
        this.ctx.font = "34px YouYuan";
        this.ctx.shadowBlur = 10;
        this.ctx.shadowColor = "white";
        this.ctx.fillStyle = "rgba(0,0,0," + this.alpha + ")";
        this.ctx.fillText("恭喜您通关了！", this.width*0.5 - 80, this.height*0.4);
    }
    this.ctx.restore();

};
Niubi.prototype.init = function(){
    this.preload();
    this.create();

};
Niubi.prototype.start = function(){
    this.gameLoop();
};
Niubi.prototype.restart = function(){
    hint.style.display = "none";
    this.score = 0;
    this.sprite = [];
    this.buliding = [];
    this.create();
    this.gameOver = false;
};
Niubi.prototype.stop = function(){
    //window.cancelAnimationFrame(this.T);
    this.gameOver = true;
};
var getRandom = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
};
